package net.strudelline.fanorona;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.util.Random;

import edu.alaska.uaa.cs401.fgm.FanoronaBoardInterface;
import edu.alaska.uaa.cs401.fgm.FanoronaColor;
import edu.alaska.uaa.cs401.fgm.FanoronaLinkBoard;
import edu.alaska.uaa.cs401.fgm.FanoronaMoveException;

public class FanoronaRandomAI {
	private Socket sock;
	private BufferedReader sr;
	private BufferedWriter sw;
	private FanoronaColor color = null;
	FanoronaBoardInterface board = new FanoronaLinkBoard();
	Random rng = new Random();
	
	public FanoronaRandomAI(String host,int port) throws IOException {
		sock = new Socket();
		sock.connect(new InetSocketAddress(host,port));
		sr = new BufferedReader(new InputStreamReader(sock.getInputStream(),"US-ASCII"));
		sw = new BufferedWriter(new OutputStreamWriter(sock.getOutputStream(),"US-ASCII"));
	}
	
	public void run() throws IOException {
		switch (rng.nextInt(5)) {
		case 0:
			sw.write("ned\n");
			break;
		case 1:
			sw.write("ted\n");
			break;
		case 2:
			sw.write("fred\n");
			break;
		case 3:
			sw.write("zed\n");
			break;
		default:
			sw.write("jed\n");
			break;
		}
		sw.flush();
		String line = sr.readLine();
		if (line.startsWith("+black")) {
			color = FanoronaColor.Black;
		} else {
			color = FanoronaColor.White;
			makeAMove();
		}
		while (true) {
			line = sr.readLine();
			if (line == null) break;
			if (line.startsWith("-")) {
				try {
					board.processMove(line.substring(1),color.opposite());
				} catch (FanoronaMoveException e) {
					System.err.println("Server sent an invalid move: "+line);
				}
				System.out.format("%s won.", board.getWinner());
				break;
			} else {
				try {
					board.processMove(line.substring(1),color.opposite());
				} catch (FanoronaMoveException e) {
					System.err.println("Server sent an invalid move: "+line);
				}				
			}
			makeAMove();
		}
	}
	
//	private boolean onBoard(int x, int y) {
//		if (x < 0) return false;
//		if (x > 8) return false;
//		if (y < 0) return false;
//		if (y > 4) return false;
//		return true;
//	}
	
	public void makeAMove() throws IOException {
		int xoff = rng.nextInt(9);
		int yoff = rng.nextInt(5);
		for (int xstep = 0; xstep < 9; xstep++) {
			int x = (xoff + xstep) % 9;
			for (int ystep = 0; ystep < 5; ystep++) {
				int y = (yoff + ystep) % 5;
				
				for (int dx = -1; dx < 2; dx++) {
					for (int dy = -1; dy < 2; dy++) {
						for (String mt : new String [] {"M","C","W"}) {
							String move = String.format("%s %d %d %d %d",mt,x,y,x+dx,y+dy); 
							try {
								board.processMove(move, color);
								sw.write(move+"\n");
								sw.flush();
								if (sr.readLine().startsWith("-")) { 
									System.out.println("server says game over!");
								}
								return;
							} catch (FanoronaMoveException e) {
							}
						}
					}
				}
			}
		}
	}
	public static void main(String[] argv) throws IOException {
		FanoronaRandomAI ai = new FanoronaRandomAI("localhost",3266);
		ai.run();
	}
}
